import { onMounted, ref } from 'vue'
import { getSearchAll } from '@/apis/goods.js'
import { useRoute } from 'vue-router'

export function useCategoryGoodsTemporary() {
  const route = useRoute()
  const searchObj = ref({
    categoryId: route.params.id,
    page: 1,
    pageSize: 18,
    sortField: 'publishTime',
    sortMethod: 'desc',
  })
  const categoryGoodsTemporary = ref({})
  const handleClick = async () => {
    searchObj.value.page = 1
    categoryGoodsTemporary.value = await getSearchAll(searchObj.value)
  }
  onMounted(async () => {
    categoryGoodsTemporary.value = await getSearchAll(searchObj.value)
  })
  const load = async () => {
    searchObj.value.page++
    const result = await getSearchAll(searchObj.value)
    categoryGoodsTemporary.value.items = [...categoryGoodsTemporary.value.items, ...result.items]
  }
  return {
    searchObj,
    categoryGoodsTemporary,
    handleClick,
    load,
  }
}
